<?php
/**
* Bysoft
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com and you will be sent a copy immediately.
*
* @category   Bysoft
* @package    Bysoft_Flashnavigator
* @author     Bysoft
* @copyright  Copyright (c) 2009 Bysoft (http://www.bysoft.fr)
* @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
*/ 


class Bysoft_Flashnavigator_Block_Adminhtml_Flashnavigator_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
    protected function _prepareForm()
    {
        
        $form = new Varien_Data_Form();
        $this->setForm($form);
        $fieldset = $form->addFieldset('flashnavigator_form', array('legend'=>Mage::helper('flashnavigator')->__('Information')));
        $id = (int) $this->getRequest()->getParam('id');
        $intStoreId = (int) $this->getRequest()->getParam('store');
        
        $filename="slideshow/items/default/settings.xml";
        if( $id > 0 && file_exists( Mage::getBaseDir('media')."/"."slideshow/items/{$id}/settings.xml" ) && $intStoreId == 0 ){
            $filename="slideshow/items/{$id}/settings.xml";
            
        }else if( $id > 0 && file_exists( Mage::getBaseDir('media')."/"."slideshow/items/{$id}/{$intStoreId}/settings.xml" ) && $intStoreId > 0 ){
            $filename="slideshow/items/{$id}/{$intStoreId}/settings.xml";
            
        }else if( $id > 0 && !file_exists( Mage::getBaseDir('media')."/"."slideshow/items/{$id}/{$intStoreId}/settings.xml" ) && $intStoreId > 0 ){
            if( file_exists( Mage::getBaseDir('media')."/"."slideshow/items/{$id}/settings.xml" ) ){
                $filename="slideshow/items/{$id}/settings.xml";
            }
            
        }
        
        $flashnavigator_model = Mage::getModel("flashnavigator/flashnavigator");
        $flash_data = $flashnavigator_model->load( $id );        
        
        $strFilePath = Mage::getBaseDir('media')."/".$filename;
        
    //if the file is not exist, popup a message.
    /*
    if(!file_exists($strFilePath)) {
        $strSourceFolder = Mage::getBaseDir('media')."/slideshow/banner_720x180/";
        $steDestFolder   = Mage::getBaseDir('media')."/slideshow/{$intStoreId}/";
        mkdir($steDestFolder."img", 0777, true);
        copy($strSourceFolder."banner_720x180.swf", $steDestFolder."banner_720x180.swf");
        copy($strSourceFolder."AC_RunActiveContent.js", $steDestFolder."AC_RunActiveContent.js");
        copy($strSourceFolder."settings.xml", $steDestFolder."settings.xml");
        copy($strSourceFolder."index.html", $steDestFolder."index.html");
    }
    */
    
    $fieldset->addField('store', 'hidden', array(
    'label'     => '',
    'name'      => 'store',
    'value'     => $intStoreId,
    ));
    
   if( $intStoreId > 0 ){
        $fieldset->addField('title', 'text', array(
        'label'     => Mage::helper('flashnavigator')->__('Title'),
        'class'     => 'required-entry',
        'required'  => true,
        'readonly'  => 'readonly',
        'name'      => 'title',
        'value'     => $flash_data->getData('title'),
    ));
    }else{
       $fieldset->addField('title', 'text', array(
        'label'     => Mage::helper('flashnavigator')->__('Title'),
        'class'     => 'required-entry',
        'required'  => true,
        'name'      => 'title',
        'value'     => $flash_data->getData('title'),
    ));
    }
    
    
    if( $flash_data->getData('id') > 0 ){
        $fieldset->addField('id', 'hidden', array(            
            'class'     => 'required-entry',
            'required'  => true,
            'name'      => 'id',
            'value'     => $flash_data->getData('id'),
        ));
    }
    
    $xml = simplexml_load_file( $strFilePath );

	for($i=0;$i<count($xml->links->link);$i++){

		$fieldset->addField('order'.$i, 'hidden', array(
			'label'     => Mage::helper('flashnavigator')->__('Order'),
			'name'      => 'order'.$i,
			'value'     => $i,
		));

        
        
        $fieldset->addField('title'.$i, 'text', array(
            'label'     => Mage::helper('flashnavigator')->__('URL '.($i+1)),
            'class'     => 'required-entry',
            'required'  => true,
            'name'      => 'title'.$i,
            'value'     => $xml->links->link[$i]->link_url,
        ));
        // because at the url inside defult setting.xml is relative.
        $image_url = stristr( $xml->links->link[$i]->background_image,'http://' ) ? $xml->links->link[$i]->background_image:eregi_replace( '\/index\.php\/?','', Mage::getBaseUrl('web') ) . $xml->links->link[$i]->background_image;
        $real_image_location =  stristr( $xml->links->link[$i]->background_image,'http://' ) ? str_replace( eregi_replace( '\/index\.php\/?','', Mage::getBaseUrl('web') ),'', $xml->links->link[$i]->background_image ):$xml->links->link[$i]->background_image;         
       
        $fieldset->addField('filename'.$i, 'image', array(
            'label'     => Mage::helper('flashnavigator')->__('File')." ".($i+1),
            'required'  => false,
            'name'      => 'filename'.$i,
            'value'     => $xml->links->link[$i]->background_image != '' && file_exists( $real_image_location ) ? $image_url:'',
        ));

    }

          $fieldset->addField('displayduration', 'text', array(
          'label'     => Mage::helper('flashnavigator')->__('Display duration'),
          'name'      => 'displayduration',
          'value'     => $xml->settings->DISPLAY_DURATION,
      ));

          $fieldset->addField('transtionduration', 'text', array(
          'label'     => Mage::helper('flashnavigator')->__('Transition duration'),
          'name'      => 'transtionduration',
          'value'     => $xml->settings->TRANSITION_DURATION,
      ));

      /*$fieldset->addField('content', 'editor', array(
          'name'      => 'content',
          'label'     => Mage::helper('flashnavigator')->__('Content'),
          'title'     => Mage::helper('flashnavigator')->__('Content'),
          'style'     => 'width:700px; height:500px;',
          'wysiwyg'   => false,
          'required'  => true,
      ));*/

    if( $intStoreId ==  0 ){
     $strBlockCode = <<<EOM
        <script src="media/slideshow/includes/AC_RunActiveContent.js" type="text/javascript"></script>
        <script type="text/javascript">
        
        AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0','width','720','height','180','src','media/slideshow/items/<!--current_id-->','quality','high','wmode','transparent','pluginspage','http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash','movie','media/slideshow/items/<!--current_id-->' ); //end AC code
        
        </script><noscript><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="720" height="180">
        
                <param name="movie" value="media/slideshow/items/<!--current_id-->/banner_720x180.swf" />
                <param name="wmode" value="transparent" />
                <param name="quality" value="high" />
        
                <embed src="media/slideshow/items/<!--current_id-->/banner_720x180.swf" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="720" height="180" wmode="transparent"></embed>
        
        </object></noscript>     
EOM;
    }else{
         $strBlockCode = <<<EOM
        <script src="media/slideshow/includes/AC_RunActiveContent.js" type="text/javascript"></script>
        <script type="text/javascript">
        
        AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0','width','720','height','180','src','media/slideshow/items/<!--current_id-->/{$intStoreId}','quality','high','wmode','transparent','pluginspage','http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash','movie','media/slideshow/items/<!--current_id-->/{$intStoreId}' ); //end AC code
        
        </script><noscript><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="720" height="180">
        
                <param name="movie" value="media/slideshow/items/<!--current_id-->/{$intStoreId}/banner_720x180.swf" />
                <param name="wmode" value="transparent" />
                <param name="quality" value="high" />
        
                <embed src="media/slideshow/items/<!--current_id-->/{$intStoreId}/banner_720x180.swf" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="720" height="180" wmode="transparent"></embed>
        
        </object></noscript>     
EOM;
    }
     if( $id > 0 ){
         $strBlockCode = str_replace( '<!--current_id-->',$id,$strBlockCode );
     }
     $fieldset->addField('block_code', 'textarea', array(
              'label'     => Mage::helper('flashnavigator')->__('Static Block Code'),
              'name'      => 'code',
              'value'     => $strBlockCode,
              'style'     => 'width:700px; height:500px;',
          ));

      if ( Mage::getSingleton('adminhtml/session')->getFlashnavigatorData() )
      {
          $form->setValues(Mage::getSingleton('adminhtml/session')->getFlashnavigatorData());
          Mage::getSingleton('adminhtml/session')->setFlashnavigatorData(null);
      } elseif ( Mage::registry('flashnavigator_data') ) {
          $form->setValues(Mage::registry('flashnavigator_data')->getData());
      }
      return parent::_prepareForm();

  }




}